I have written a function to remove new lines and semi columns. The function iterates all chars in a block of text. In our application, as a whole, the consumes 6 minutes. Is there a way to to optimize the function?
string remNewLine(string str) SystemAdmin - Thu Apr 23 04:54:39 EDT 2009 |
Re: Optimization of a function
Would this work better:
string remNewLine(string str)
{
Buffer buf_in = create
buf_in = str
int i_len = length(buf_in)
int i = 0
for (i=0; i < i_len; i++)
{
if ((buf_in[i] == '\n') || (buf_in[i] == ';'))
{
set(buf_in,i,' ')
}
}
str = stringOf(buf_in)
delete buf_in
buf_in = null
return str
}
It uses the internal set function to replace characters and thus needs only one buffer and also clears that after use, so the buffers are not left in memory.
|
Re: Optimization of a function SystemAdmin - Thu Apr 23 07:33:54 EDT 2009
Would this work better:
string remNewLine(string str)
{
Buffer buf_in = create
buf_in = str
int i_len = length(buf_in)
int i = 0
for (i=0; i < i_len; i++)
{
if ((buf_in[i] == '\n') || (buf_in[i] == ';'))
{
set(buf_in,i,' ')
}
}
str = stringOf(buf_in)
delete buf_in
buf_in = null
return str
}
It uses the internal set function to replace characters and thus needs only one buffer and also clears that after use, so the buffers are not left in memory.
|
Re: Optimization of a function
Yes, using the same buffer works a little better, but only because you are replacing characters with exactly one other character. If you want to replace all the EOLs with two spaces, they you need your solution.
Buffer remNewLine_BufIn = create()
Buffer remNewLine_BufOut = create()
string remNewLine(string str)
{ remNewLine_BufIn = str
remNewLine_BufOut = ""
bla bla bla
return(stringOf(remNewLins_BufOut)
} // end remNewLine()
|